package mail.selenium;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import static com.han.http.util.Print.printlnInfo;

/**
 * @author hanjiafeng
 */
public class Selenium {

    public static void main(String... args) {
        Map<String, String> s = mailQqSlide("F:\\workspace\\java\\MailCrawler\\src\\main\\resources\\cache\\cap_union_new1551772502601.html");
        printlnInfo(s);
    }

    public static String mail21CNEncode(String pwd, String rsaKey) {
        //初始化驱动,并设置为允许使用js
        HtmlUnitDriver driver = new HtmlUnitDriver(true);
        //设置页面地址
        driver.get("file:\\F:\\workspace\\java\\MailCrawler\\src\\main\\resources\\Ht21CN.html");
        //获取密码输入框目标
        WebElement pwdEle = driver.findElement(By.id("pwd"));
        //清除内容
        pwdEle.clear();
        //设置内容
        pwdEle.sendKeys(pwd);
        //获取rsa秘钥框
        WebElement rsaKeyEle = driver.findElement(By.id("rsaKey"));
        rsaKeyEle.clear();
        rsaKeyEle.sendKeys(rsaKey);
        //执行js
        return "{RSA}" + driver.executeScript("return encode('" + pwd + "','" + rsaKey + "')");
        /*return "{RSA}" + driver.executeScript("    var JSEncrypt111 = JSEncryptExports.JSEncrypt;\n" +
                "    var encrypt = new JSEncrypt111;\n" +
                "    var para1 = document.getElementById(\"pwd\").value;\n" +
                "    var para2 = document.getElementById(\"rsaKey\").value;\n" +
                "    encrypt.setKey(para2);\n" +
                "    return encrypt.encrypt(para1)").toString();*/
    }

    public static String mailQqEncode(String pwd, String encryptCode, String verifyCode) {

        //初始化驱动,并设置为允许使用js
        HtmlUnitDriver driver = new HtmlUnitDriver(true);
        //设置页面地址
        driver.get("file:\\F:\\workspace\\java\\MailCrawler\\src\\main\\resources\\HtQq.html");

        //执行js
        Object encryptedPwd = driver.executeScript("return encode1('" + pwd + "','" + encryptCode + "','" + verifyCode + "');");
        return encryptedPwd.toString();
    }

    public static Map<String, String> mailQqSlide(String htmlFilePath) {

        System.setProperty("webdriver.chrome.driver", "A:\\SelfTest\\chromedriver.exe");
        ChromeDriver driver = new ChromeDriver();
        driver.get("file:\\" + htmlFilePath);

        Map<String, String> slideParameterMap = new HashMap<>(9);
        int retryCount = 10;
        //缺口位置
        WebElement ansCodeEle;
        String ansCode = null;
        WebElement vsigEle;
        String vsig = null;
        WebElement cdataEle;
        String cdata = null;
        WebElement websigEle;
        String websig = null;
        WebElement subcapclassEle;
        String subcapclass = null;
        WebElement encryptedTrackEle;
        String encryptedTrack = null;
        WebElement fpinfoEle;
        String fpinfo = null;
        WebElement eksEle;
        String eks = null;
        WebElement tlgEle;
        String tlg = null;

        //最多重复10次,每次等待5秒,到时间后如果还没有获取到,跳出,返回null
        while ((Objects.isNull(ansCode) || Objects.isNull(vsig) || Objects.isNull(cdata) || Objects.isNull(websig)
                || Objects.isNull(subcapclass) || Objects.isNull(encryptedTrack) || Objects.isNull(fpinfo)
                || Objects.isNull(eks) || Objects.isNull(tlg)) && retryCount-- > 0) {
            try {
                Thread.sleep(1000 * 3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            try {
                ansCodeEle = driver.findElementById("vertexCode");
                ansCode = ansCodeEle.getText();
                slideParameterMap.put("vertexCode", ansCode);

                vsigEle = driver.findElementById("vsigCode");
                vsig = vsigEle.getText();
                slideParameterMap.put("vsigCode", vsig);

                cdataEle = driver.findElementById("cdataCode");
                cdata = cdataEle.getText();
                slideParameterMap.put("cdataCode", cdata);

                websigEle = driver.findElementById("websigCode");
                websig = websigEle.getText();
                slideParameterMap.put("websigCode", websig);

                subcapclassEle = driver.findElementById("subcapclassCode");
                subcapclass = subcapclassEle.getText();
                slideParameterMap.put("subcapclassCode", subcapclass);

                encryptedTrackEle = driver.findElementById("encryptedCode");
                encryptedTrack = encryptedTrackEle.getText();
                slideParameterMap.put("encryptedCode", encryptedTrack);

                fpinfoEle = driver.findElementById("fpinfoCode");
                fpinfo = fpinfoEle.getText();
                slideParameterMap.put("fpinfoCode", fpinfo);

                eksEle = driver.findElementById("eksCode");
                eks = eksEle.getText();
                slideParameterMap.put("eksCode", eks);

                tlgEle = driver.findElementById("tlgCode");
                tlg = tlgEle.getText();
                slideParameterMap.put("tlgCode", tlg);
            } catch (NoSuchElementException ignore) {
            }
        }
        driver.close();

        return slideParameterMap;
    }
}
